package com.zues.thread.aqs;

import com.zues.thread.util.ThreadUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName ConcurrentHashMapDemo2
 * @Description TODO
 * @Author zeus
 * @Date 4/3/2025 10:01 上午
 * @Version 1.0
 **/
@Slf4j(topic = "c.ConcurrentHashMapDemo2")
public class ConcurrentHashMapDemo2 {

    public static Map<String, String> map = new ConcurrentHashMap();

    //使用HashMap的话结果为1051321，线程不安全
//    public static Map<String, String> map = new HashMap<>();

    public static void main(String[] args) {
        new AddMapDataThread().start();
        new AddMapDataThread().start();

        ThreadUtil.sleep(5);//休息5秒，确保两个线程执行完毕

        log.debug("Map的大小： {}", map.size());
    }

}


class AddMapDataThread extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 1000000; i++) {
            ConcurrentHashMapDemo2.map.put("键：" + i, "值" + i);
        }
    }
}
